package com.xxl.job.executor.service.impl;

import com.alibaba.fastjson.JSON;
import com.xxl.job.core.constants.Constants;
import com.xxl.job.executor.dao.WebReportMapper;
import com.xxl.job.executor.model.WebReport;
import com.xxl.job.executor.service.WebReportApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WebReportApiServiceImpl implements WebReportApiService {

    private Logger logger = LoggerFactory.getLogger(WebReportApiServiceImpl.class);

    @Autowired(required = false)
    private WebReportMapper webReportMapper;

    /**
     * 所有统计
     *
     * @return
     */
    @Override
    public String getAllTotal() {

        Map<String, String> map = new HashMap<>();
        map.put("model", Constants.WEB_REPORT_MODEL);
        map.put("type", Constants.WEB_REPORT_TYPE1);

        List<WebReport> webReports = webReportMapper.queryAllTotal(map);
        logger.info("所有统计：{}", JSON.toJSONString(webReports));

        return JSON.toJSONString(webReports);
    }

    /**
     * 应用请求服务排名
     *
     * @return
     */
    @Override
    public String getAppRequestRank() {

        Map<String, String> map = new HashMap<>();
        map.put("model", Constants.WEB_REPORT_MODEL);
        map.put("type", Constants.WEB_REPORT_TYPE2);
        map.put("name", Constants.APPLICATION_REQUEST_RANK);
        map.put("key", Constants.APPLICATION_REQUEST_RANK);

        List<WebReport> webReports = webReportMapper.query(map);
        logger.info("应用请求服务排名：{}", JSON.toJSONString(webReports));
        WebReport webReport = webReports.get(0);

        return JSON.toJSONString(webReport.getValue());
    }

    /**
     * 应用订阅服务排名
     *
     * @return
     */
    @Override
    public String getAppSubscribeRank() {

        Map<String, String> map = new HashMap<>();
        map.put("model", Constants.WEB_REPORT_MODEL);
        map.put("type", Constants.WEB_REPORT_TYPE2);
        map.put("name", Constants.APPLICATION_SUBSCRIBE_RANK);
        map.put("key", Constants.APPLICATION_SUBSCRIBE_RANK);

        List<WebReport> webReports = webReportMapper.query(map);
        logger.info("应用订阅服务排名：{}", JSON.toJSONString(webReports));
        WebReport webReport = webReports.get(0);

        return JSON.toJSONString(webReport.getValue());
    }

    /**
     * 部门订阅服务排名
     *
     * @return
     */
    @Override
    public String getDepartSubscribeRank() {

        Map<String, String> map = new HashMap<>();
        map.put("model", Constants.WEB_REPORT_MODEL);
        map.put("type", Constants.WEB_REPORT_TYPE2);
        map.put("name", Constants.DEPARTMENT_SUBSCRIBE_RANK);
        map.put("key", Constants.DEPARTMENT_SUBSCRIBE_RANK);

        List<WebReport> webReports = webReportMapper.query(map);
        logger.info("部门订阅服务排名：{}", JSON.toJSONString(webReports));
        WebReport webReport = webReports.get(0);

        return JSON.toJSONString(webReport.getValue());
    }

    /**
     * 部门调用服务排名
     *
     * @return
     */
    @Override
    public String getDepartRequestRank() {

        Map<String, String> map = new HashMap<>();
        map.put("model", Constants.WEB_REPORT_MODEL);
        map.put("type", Constants.WEB_REPORT_TYPE2);
        map.put("name", Constants.DEPARTMENT_REQUEST_RANK);
        map.put("key", Constants.DEPARTMENT_REQUEST_RANK);

        List<WebReport> webReports = webReportMapper.query(map);
        logger.info("部门调用服务排名：{}", JSON.toJSONString(webReports));
        WebReport webReport = webReports.get(0);

        return JSON.toJSONString(webReport.getValue());
    }

    /**
     * 服务授权应用排名
     *
     * @return
     */
    @Override
    public String getAuthorizeApplicationRank() {

        Map<String, String> map = new HashMap<>();
        map.put("model", Constants.WEB_REPORT_MODEL);
        map.put("type", Constants.WEB_REPORT_TYPE2);
        map.put("name", Constants.AUTHORIZE_APPLICATION_RANK);
        map.put("key", Constants.AUTHORIZE_APPLICATION_RANK);

        List<WebReport> webReports = webReportMapper.query(map);
        logger.info("服务授权应用排名：{}", JSON.toJSONString(webReports));
        WebReport webReport = webReports.get(0);

        return JSON.toJSONString(webReport.getValue());
    }
}
